home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14504 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: mail2news.demon.co.uk!willen.demon.co.uk
  2. From: Adrian Parker <adrian@willen.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: 2d array of pointers to structures ?
  5. Date: Mon, 15 Apr 96 13:04:08 GMT
  6. Organization: Quondam Ltd
  7. Message-ID: <829573448snz@willen.demon.co.uk>
  8. Reply-To: adrian@willen.demon.co.uk
  9. X-NNTP-Posting-Host: willen.demon.co.uk
  10. X-Mail2News-Path: willen.demon.co.uk
  11.  
  12.  
  13. I need to keep a 2d array of structures, which I have to do at runtime 
  14. as they array could be quite big, and the compiler can't cope with 
  15. static arrays of the required size.
  16.  
  17. Say I have a structure of..
  18.  
  19.     typedef struct {
  20.         int id;
  21.         long colour;
  22.     } T_loc;
  23.     T_loc *locptr;
  24.  
  25. And two variables containing the number of rows and colums.   How do I
  26. allocate the storage for the memory array, and how then do I reference
  27. it ?
  28.  
  29. I know I need to allocate a pointer for each row, so..
  30.  
  31.     locptr = (T_loc *)malloc(rows);
  32.  
  33. should allocate enough space to hold "rows" number of T_loc pointers.  
  34. Then in each entry, I need to store a pointer to an array of columns..
  35. which is where I'm stuck.. 
  36.  
  37.     for (x = 0 ; x < rows ; x++)
  38.     {
  39.         *(locptr+x) = (T_loc)malloc(cols);
  40.     }
  41.  
  42. which I think should  malloc "cols" number of T_loc variables, and store
  43. the returned pointer in the 'x'th locptr pointer array..  but obviously
  44. something is wrong as it won't compile.
  45.  
  46. I then need to be able to refer to a specific struct items for a
  47. specific column in a specific row..
  48.  
  49.     (T_loc *)((*(locptr+ROW))+COL)->id
  50.  
  51. or am I way off here too ?
  52.  
  53.  
  54. Any help would be appreciated.
  55.  
  56. Regards,
  57. confused of willenhall
  58. -- 
  59. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  60. :: Adrian Parker  adrian@willen.demon.co.uk  Quondam Ltd ::
  61. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  62.